package com.shire.shire_interface.controller.group;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.group.WxGroupMember;
import com.shire.shire_interface.model.group.WxGroupMemberExample;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.account.WxAccountService;
import com.shire.shire_interface.service.commandcenter.BaseCommandProcessor;
import com.shire.shire_interface.service.commandcenter.WxCommandService;
import com.shire.shire_interface.service.group.WxGroupMemberService;
import com.shire.shire_interface.service.group.WxGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

//@Api(tags = "群管理 群成员管理")
@RestController
@RequestMapping("/group")
public class WxGroupMemberManagerController extends BaseController {

    @Resource
    private WxGroupService wxGroupService;
    @Resource
    private WxGroupMemberService wxGroupMemberService;
    @Resource
    private WxAccountService accountService;
    @Resource
    private WxCommandService commandService;
    @Resource
    private BaseCommandProcessor commandProcessor;


//    @ApiOperation(value = "群成员列表 csId, wxAccount, groupNo")
    @ResponseBody
    @PostMapping(value = "/memberList")
    public Response groupMemberList(@RequestBody JSONObject jsonObject, HttpSession session) {

        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            String groupNo = jsonObject.getString("groupNo");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxGroupMemberExample example = new WxGroupMemberExample();
            example.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(groupNo);
            List<WxGroupMember> wxGroupMembers = wxGroupMemberService.groupMembers(example);
            response.setData(wxGroupMembers);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }


//    @ApiOperation(value = "解散群 csId, wxAccount, groupNo")
//    @ResponseBody
//    @PostMapping(value = "/dissGroup")
//    public Response dissGroup(@RequestBody JSONObject jsonObject, HttpSession session){
//        Response response = new Response();
//        try {
//
//            Integer csId = jsonObject.getInteger("csId");
//            String wxAccount = jsonObject.getString("wxAccount");
//            String groupNo = jsonObject.getString("groupNo");
//            if (!checkLoginUserByCsId(session, csId)){
//                response.error("请登录！");
//                response.setCode(501);
//                return response;
//            }
//            WxGroupExample wxGroupExample = new WxGroupExample();
//            wxGroupExample.createCriteria().andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(groupNo);
//            List<WxGroup> wxGroups = wxGroupService.selectByExample(wxGroupExample);
//            for (WxGroup wxGroup1 : wxGroups) {
//                WxGroupMemberExample example = new WxGroupMemberExample();
//                example.createCriteria().andGroupidEqualTo(wxGroup1.getId());
//                wxGroupMemberService.deleteByExample(example);
//            }
//
//            wxGroupService.deleteByExample(wxGroupExample);
//            response.success("成功");
//        } catch (Exception e) {
//            response.error("失败");
//            e.printStackTrace();
//        }
//        return response;
//    }

//    @ApiOperation(value = "踢出群 csId, wxAccount, groupNo, memberWxAccount")
    @ResponseBody
    @PostMapping(value = "/removeGroup")
    public Response removeGroup(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            String groupNo = jsonObject.getString("groupNo");
            String memberWxAccount = jsonObject.getString("memberWxAccount");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxGroupMemberExample example = new WxGroupMemberExample();
            example.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(groupNo).andWxidEqualTo(memberWxAccount);
            wxGroupMemberService.deleteByExample(example);

            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "邀请好友进群 csId, wxAccount, friendWxAccountList， groupNo")
    @ResponseBody
    @PostMapping(value = "/addFriend2Group")
    public Response addFriend2Group(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxAccount = jsonObject.getString("wxAccount");
            List<String> friendWxAccountList = (List<String>) jsonObject.get("friendWxAccountList");
            String groupNo = jsonObject.getString("groupNo");
//
//            WxGroupMember wxGroupMember = new WxGroupMember();
//            wxGroupMember.setCsid(csId);
//            wxGroupMember.setWxaccount(wxAccount);
//            wxGroupMember.setGroupno(groupNo);
//
//            for (String friendWxAccount : friendWxAccountList) {
//                wxGroupMember.setWxid(friendWxAccount);
//                wxGroupMemberService.insertBySelective(wxGroupMember);
//            }
            WxAccount account = accountService.getWxAccountByWxId(wxAccount);
            JSONObject reqObj = new JSONObject();
            reqObj.put("groupWxId", groupNo);
            reqObj.put("userWxIds", String.join(",",friendWxAccountList));
            reqObj.put("inviteMessage", "");

            WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),account.getId(),reqObj.toJSONString(), WxConstants.CmdTaskType.UNKNOWN, -1, WxConstants.CmdType.CRK_INVITE_GROUP_D,WxConstants.CmdPriority.UNKNOWN);
            commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(),command);

            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
